package com.demo.widget.meis;

import com.demo.widget.BaseAbility;
import com.demo.widget.ResourceTable;
import com.meis.widget.horizontal.HorizontalScrollView;

import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wenshi on 2018/12/13.
 * Description
 */
public class MeiScrollViewAbility extends BaseAbility {
    private HorizontalScrollView mScrollView;
    private ListContainer mRecyclerView;
    private ToastDialog toast;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_mei_scroll_view_ability);
        mScrollView = (HorizontalScrollView) findComponentById(ResourceTable.Id_scroll_view);
        mRecyclerView = (ListContainer) findComponentById(ResourceTable.Id_recycler);
        mRecyclerView.setOrientation(Component.HORIZONTAL);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            list.add("");
        }
        mRecyclerView.setItemProvider(new CardAdapter(getContext(), list));
        toast = createToast();
        mScrollView.setOnReleaseListener(() -> {
            if (toast.isShowing()) toast.destroy();
            Text toastText = createText();
            toastText.setText("触发了查看更多回调接口");
            toast.setComponent(toastText);
            toast.registerDisplayCallback(iDialog -> {
                ComponentParent parent = toastText.getComponentParent();
                if (parent != null) setBG((Component) parent, 0x00000000);
            });
            toast.show();
        });
    }

    private ToastDialog createToast() {
        ToastDialog toast = new ToastDialog(getContext());
        toast.setSize(640, 150);
        toast.setOffset(0, 180);
        toast.setAlignment(LayoutAlignment.BOTTOM);
        toast.setDuration(2000);
        return toast;
    }

    private Text createText() {
        Text text = new Text(getContext());
        text.setTextSize(40);
        text.setWidth(640);
        text.setHeight(150);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setTextColor(Color.BLACK);
        setBG(text, 0xFFF0F0F0);
        return text;
    }

    private void setBG(Component component, int color) {
        if (component == null) return;
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(0);
        shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
        shapeElement.setCornerRadius(75);
        component.setBackground(shapeElement);
    }

    private static class CardAdapter extends BaseItemProvider {
        private Context mContext;
        private List<String> mDatas;

        public CardAdapter(Context mContext, List<String> mDatas) {
            this.mContext = mContext;
            this.mDatas = mDatas;
        }

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public Object getItem(int i) {
            return mDatas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
            return LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_mei_item_card,
                    null, false);
        }
    }
}
